package com.aplink.generic.xmlparser;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.content.res.Resources;
import android.text.TextUtils;

import com.aplink.generic.constant.type.ActionType;
import com.aplink.generic.constant.type.ControlStatus;
import com.aplink.generic.constant.type.ControlType;
import com.aplink.generic.constant.type.EditTextType;
import com.aplink.generic.constant.type.ListViewSelection;
import com.aplink.generic.dialog.ApLinkProgressDialog;
import com.aplink.generic.pojo.Value;
import com.aplink.generic.pojo.ValueList;
import com.aplink.generic.pojo.controlspec.AutoCompleteTextViewSpec;
import com.aplink.generic.pojo.controlspec.ButtonSpec;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.EditTextSpec;
import com.aplink.generic.pojo.controlspec.GallerySpec;
import com.aplink.generic.pojo.controlspec.GoogleMapsSpec;
import com.aplink.generic.pojo.controlspec.ImageViewSpec;
import com.aplink.generic.pojo.controlspec.ListviewSpec;
import com.aplink.generic.pojo.controlspec.RadioButtonSpec;
import com.aplink.generic.pojo.controlspec.actionbar.ActionBarSpec;
import com.aplink.generic.pojo.controlspec.gridview.GridViewSpec;
import com.aplink.generic.pojo.controlspec.tabhost.TabHostSpec;
import com.aplink.generic.pojo.controlspec.textview.TextViewSpec;
import com.aplink.generic.pojo.formdesign.FormDesign;
import com.aplink.generic.pojo.formdesign.InitValue;
import com.aplink.generic.pojo.formdesign.Layout;
import com.aplink.generic.util.ResourcesHelper;
import com.aplink.generic.util.objectrule.EnableCondition;
import com.aplink.generic.util.objectrule.ObjectBaseRule;
import com.aplink.generic.util.objectrule.TargetCollection;
import com.aplink.generic.util.validation.Validation;
import com.aplink.generic.xmlparser.ctrlformat.AutoCompleteTextViewFormat;
import com.aplink.generic.xmlparser.ctrlformat.CheckBoxFormat;
import com.aplink.generic.xmlparser.ctrlformat.DateTimeFormat;
import com.aplink.generic.xmlparser.ctrlformat.EditTextFormat;
import com.aplink.generic.xmlparser.ctrlformat.GalleryFormat;
import com.aplink.generic.xmlparser.ctrlformat.GoogleMapsFormat;
import com.aplink.generic.xmlparser.ctrlformat.GridViewFormat;
import com.aplink.generic.xmlparser.ctrlformat.ListViewFormat;
import com.aplink.generic.xmlparser.ctrlformat.SpinnerFormat;
import com.aplink.generic.xmlparser.ctrlformat.SwitchFormat;
import com.aplink.generic.xmlparser.ctrlformat.TabHostFormat;
import com.aplink.generic.xmlparser.ctrlformat.ToggleFormat;

public class FormDesignParser {
	private static final String ACTION = "action";
	private static final String ANIMATION = "animation";
	private static final String ANIMATION_IN = "animation_in";
	private static final String ANIMATION_OUT = "animation_out";
	private static final String AUTOCOMPLETE_TEXTVIEW_FORMAT = "AutoCompleteTextViewFormat";
	private static final String BACKGROUND_COLOR = "backgroundColor";
	private static final String CHECKBOX_FORMAT = "CheckBoxFormat";
	private static final String CONTENTDATA = "contentdata";
	private static final String CONTROL_STATUS = "ControlStatus";
	private static final String CURRENT_PAGE_INDEX = "startpageindex";
	private static final String DATAOBJ = "dataobj";
	private static final String DATETIME_PICKER_FORMAT = "DateTimePickerFormat";
	private static final String DISPLAY = "display";
	private static final String DISPLAY_FORMAT = "displayFormat";
	private static final String DISPLAY_TYPE = "displayType";
	private static final String DISPLAYPROP = "displayprop";
	private static final String EDITTEXT_FORMAT = "EditTextFormat";
	private static final String ENABLE_NAVIGATE_DRAWER = "isEnableNavigateDrawer";
	private static final String FILTER_BY = "filterBy";
	private static final String FOOTER = "footer";
	private static final String FORM_DESIGN = "formdesign";
	private static final String GALLERY_FORMAT = "GalleryFormat";
	private static final String GOOGLE_MAPS_FORMAT = "GoogleMapFormat";
	private static final String GRIDVIEW_FORMAT = "GridViewFormat";
	private static final String HEADER = "header";
	private static final String ID = "id";
	private static final String IMAGE_PATH = "imagePath";
	private static final String INPUTTYPE = "inputtype";
	private static final String IS_ALWAY_LOAD_FIRST_RECORD = "isAlwayLoadFirstRecord";
	private static final String IS_TOGGLE_AUTO_SYNC = "isToggleAutoSync";
	private static final String IS_TOGGLE_OFFLINE_MODE = "isToggleOfflineMode";
	private static final String ISCHECKED = "checked";
	private static final String ISREADONLY = "readonly";
	private static final String ISSEARCH = "isSearch";
	private static final String LAYOUT = "layout";
	private static final String LINKING_VALUE_PROP = "linkingValueProp";
	private static final String LISTVIEW_FORMAT = "ListViewFormat";
	private static final String LOAD_ALL = "loadAll";
	private static final String MAPPINGNAME = "mappingname";
	private static final String MAXLENGTH = "maxlength";
	private static final String MINLENGTH = "minlength";
	private static final String NAME = "name";
	private static final String OBJ_LOAD_BY_CONFIG = "objloadbyconfig";
	private static final String REFRESH_CONTROL = "refreshControl";
	private static final String REFRESH_VALUE = "refreshValue";
	private static final String REGULAR_EXPRESSION = "regularExpression";
	private static final String ROWITEM = "rowitem";
	private static final String RPTRDESIGN = "rptrdesign";
	private static final String SEARCHCTRL = "searchControl";
	private static final String SEARCHFIELDS = "searchFields";
	private static final String SELECTION_MODE = "selectionMode";
	private static final String SETTINGS = "settings";
	private static FormDesignParser sInstance = null;
	private static final String SPECS = "ctlspecs";
	private static final String SPINNER_FORMAT = "SpinnerFormat";
	private static final String STYLE = "ctlstyle";
	private static final String SUBMIT_CONTROL = "submitControl";
	private static final String SWITCH_FORMAT = "SwitchFormat";
	private static final String TABHOST_FORMAT = "TabHostFormat";
	private static final String TOGGLE_FORMAT = "ToggleButtonFormat";
	private static final String TYPE = "ctltype";
	private static final String VALIDATIONGROUP = "validationgroup";
	private static final String VALUE = "value";
	private static final String VALUE_COLUMN = "valueColumn";
	private static final String VALUE_CONTROL = "valueControl";
	private static final String VALUE_LIST = "valuelist";
	private static final String VIEWPAGER_FORMAT = "ViewPagerFormat";
	private static final String VISIBILITY = "visibility";

	public static FormDesignParser getInstance() {
		synchronized (FormDesignParser.class) {
			if (FormDesignParser.sInstance == null) {
				FormDesignParser.sInstance = new FormDesignParser();
			}
		}
		return FormDesignParser.sInstance;
	}

	protected String getViewTitile(final String viewTitle) {
		return ResourcesHelper.getStringByName(viewTitle);
	}

	public FormDesign parse(final int xmlFileID) {
		if (xmlFileID == ResourcesHelper.RESOURCE_NOT_FOUND) {
			return new FormDesign();
		}
		return parserIntoControls(xmlFileID, FORM_DESIGN);
	}

	public FormDesign parse(String keyD) {
		int xmlFileID = ResourcesHelper.getRawIdByName(keyD);
		if (xmlFileID == ResourcesHelper.RESOURCE_NOT_FOUND) {
			return new FormDesign();
		}
		return parserIntoControls(xmlFileID, FORM_DESIGN);
	}

	private GridViewSpec parseGalleryFormat(Node nodeFormat) {
		final GridViewSpec galleryFormat = new GridViewSpec();
		return galleryFormat;
	}

	private FormDesign parserIntoControls(final int xmlfileId,
			final String tagname) {
		try {
			final Document doc = XmlPaserUtil.openDocumentFrom(xmlfileId);
			return parseXML(doc, tagname);
		} catch (final ParserConfigurationException e) {
			e.printStackTrace();
		} catch (final SAXException e) {
			e.printStackTrace();
		} catch (final IOException e) {
			e.printStackTrace();
		} catch (final Resources.NotFoundException e) {
			e.printStackTrace();
		}
		return new FormDesign();
	}

	private ValueList parseValueList(final Node nodeValueList) {
		final Element element = (Element) nodeValueList;
		final ValueList valueList = new ValueList();
		if (nodeValueList.hasAttributes()) {
			final String dataObj = XmlPaserUtil.getAttrValue(nodeValueList,
					DATAOBJ);
			if (dataObj != null) {
				valueList.setDataObj(dataObj);
			}

			final String submitControl = XmlPaserUtil.getAttrValue(
					nodeValueList, SUBMIT_CONTROL);

			if (submitControl != null) {
				valueList.setSubmitControl(submitControl);
			}

			final String valueControl = XmlPaserUtil.getAttrValue(
					nodeValueList, VALUE_CONTROL);

			if (valueControl != null) {
				valueList.setValueControl(valueControl);
			}

			final String valueColumn = XmlPaserUtil.getAttrValue(nodeValueList,
					VALUE_COLUMN);
			if (valueColumn != null) {
				valueList.setValueColumn(valueColumn);
			}
		}
		final NodeList nodeList = element.getElementsByTagName(VALUE);
		for (int i = 0; i < nodeList.getLength(); i++) {
			final Value value = new Value();
			final Node valueItem = nodeList.item(i);
			if (valueItem.hasAttributes()) {
				final String mappingName = XmlPaserUtil.getAttrValue(valueItem,
						MAPPINGNAME);
				value.setMappingName(mappingName);
				final String nodeValue = XmlPaserUtil.getNodeValue(valueItem);
				value.setValue(nodeValue);
				valueList.put(nodeValue, value);
			}
		}
		return valueList;
	}

	private FormDesign parseXML(final Document doc, final String tagName) {
		final FormDesign formDesign = new FormDesign();
		final NodeList nList = doc.getElementsByTagName(tagName);
		if (nList.getLength() == 0) {
			return formDesign;
		}
		Node nNode = nList.item(0);
		Element element = (Element) nNode;
		// Parse Layout element
		NodeList nodes = element.getElementsByTagName(LAYOUT);
		if (nodes.getLength() == 0) {
			return formDesign;
		}

		final Node node = nodes.item(0);
		if (node.hasAttributes()) {
			Layout layout = new Layout();
			String value = XmlPaserUtil.getAttrValue(node,
					FormDesignParser.NAME);
			if (!TextUtils.isEmpty(value)) {
				layout.setLayoutName(value);
			}
			value = XmlPaserUtil.getAttrValue(node, ANIMATION_IN);
			if (!TextUtils.isEmpty(value)) {
				layout.setAnimationIn(value);
			}

			value = XmlPaserUtil.getAttrValue(node, ANIMATION_OUT);
			if (!TextUtils.isEmpty(value)) {
				layout.setAnimationOut(value);
			}

			value = XmlPaserUtil.getAttrValue(node, ENABLE_NAVIGATE_DRAWER);
			if (!TextUtils.isEmpty(value)) {
				layout.setEnableNavigateDrawer(value.equalsIgnoreCase("Y"));
			}
			formDesign.setLayout(layout);
		}

		nodes = element.getElementsByTagName(SETTINGS);
		if (nodes.getLength() == 1) {
			if (nodes.item(0).hasAttributes()) {
				final String value = XmlPaserUtil.getAttrValue(nodes.item(0),
						IS_ALWAY_LOAD_FIRST_RECORD);
				if (!TextUtils.isEmpty(value) && value.equalsIgnoreCase("Y")) {
					formDesign.setAlwayLoadFirstRecord(true);
				}
			}
		}
		// Parse Option menu
		final ActionBarSpec optionMenuSpec = ActionBarParser.getInstance()
				.parse(element);
		formDesign.setActionBar(optionMenuSpec);
		// Parse Progress dialog
		final ApLinkProgressDialog apLinkProgressDialog = DialogParser
				.parseApLinkProgressDialog(element);
		formDesign.setApLinkProgressDialog(apLinkProgressDialog);

		// Parse AlerDialog
		formDesign.setAlertDialog(DialogParser.parseApLinkAlertDialog(element));
		formDesign.setSelectWhere(SelectWhereParser.parsse(element));
		final InitValue initValue = InitValueParser.parseInitValue(element);
		formDesign.setInitValue(initValue);
		// Parse ctlspecs element
		nodes = element.getElementsByTagName(SPECS);
		if (nodes.getLength() == 0) {
			return formDesign;
		}
		nNode = nodes.item(0);
		if (nNode.getNodeType() == Node.ELEMENT_NODE) {
			nodes = element.getElementsByTagName(STYLE);
			if (nodes.getLength() == 0) {
				return formDesign;
			}

			for (int temp = 0; temp < nodes.getLength(); temp++) {
				NodeList subNodeList = null;
				ControlSpec controlSpec = null;
				final Node nControlNode = nodes.item(temp);
				String controlType = null;
				String controlId = null;
				boolean isSearch = false;
				String searchControl = null;
				String refreshControl = null;
				String refreshValue = null;
				String searchFields = null;
				String mappingName = null;
				boolean isToggleOfflineMode = false;
				String dataInputType = null;
				boolean isVisibility = true;
				int minLength = EditTextSpec.MIN_LENGTH;
				int maxLenght = EditTextSpec.MAX_LENGTH;
				String dataObject = null;
				boolean isChecked = false;
				String contentData = null;
				String rptrDesign = null;
				String rowItem = null;
				boolean isReadOnly = false;
				String validationGroup = null;
				final ObjectBaseRule objectBaseRule = null;
				String value = null;
				int tabhostStartPageIndex = 0;
				String tabhostBackgroundColor = null;
				String tabhostDisplayStyle = null;
				boolean isLoadDataByObj = true;
				String imagePath = null;
				String header = null;
				String footer = null;
				String regex = null;
				String selectWhere = null;
				boolean isLoadAll = false;
				String displayType = null;
				String displayProperty = null;
				ValueList valueList = null;
				TargetCollection targetCollection = null;
				Validation validation = null;
				ActionType actionType = null;
				ListViewSelection listViewSelection = null;
				String linkingValueProp = null;
				boolean isToggleAutoSync = false;
				EnableCondition enableCondition = null;
				String displayFormat = null;
				String animation = null;
				ControlStatus controlStatus = null;
				if (nControlNode.hasAttributes()) {
					// find attr ctltype
					controlType = XmlPaserUtil.getAttrValue(nControlNode, TYPE);
					controlId = XmlPaserUtil.getAttrValue(nControlNode, ID);
					value = XmlPaserUtil.getAttrValue(nControlNode, VALUE);
					isSearch = false;
					if (XmlPaserUtil.getAttrValue(nControlNode, ISSEARCH) != null) {
						final String val = XmlPaserUtil.getAttrValue(
								nControlNode, ISSEARCH);
						if (val.compareToIgnoreCase("Y") == 0) {
							isSearch = true;
						}
					}
					value = XmlPaserUtil.getAttrValue(nControlNode,
							SELECTION_MODE);
					if (!TextUtils.isEmpty(value)) {
						listViewSelection = ListViewSelection.get(value);
					}
					displayFormat = XmlPaserUtil.getAttrValue(nControlNode,
							DISPLAY_FORMAT);
					displayProperty = XmlPaserUtil.getAttrValue(nControlNode,
							DISPLAYPROP);
					searchControl = XmlPaserUtil.getAttrValue(nControlNode,
							SEARCHCTRL);
					imagePath = XmlPaserUtil.getAttrValue(nControlNode,
							IMAGE_PATH);
					// find searchFields
					searchFields = XmlPaserUtil.getAttrValue(nControlNode,
							SEARCHFIELDS);
					// find mapping name
					mappingName = XmlPaserUtil.getAttrValue(nControlNode,
							MAPPINGNAME);
					// find inputType
					dataInputType = XmlPaserUtil.getAttrValue(nControlNode,
							INPUTTYPE);
					controlStatus = ControlStatus.get(XmlPaserUtil
							.getAttrValue(nControlNode, CONTROL_STATUS));
					// find inputType
					value = XmlPaserUtil.getAttrValue(nControlNode,
							IS_TOGGLE_OFFLINE_MODE);
					if (!TextUtils.isEmpty(value)
							&& value.equalsIgnoreCase("Y")) {
						isToggleOfflineMode = true;
					}
					// find inputType
					value = XmlPaserUtil.getAttrValue(nControlNode,
							IS_TOGGLE_AUTO_SYNC);
					if (!TextUtils.isEmpty(value)
							&& value.equalsIgnoreCase("Y")) {
						isToggleAutoSync = true;
					}
					// find Visibility Type
					if (XmlPaserUtil.getAttrValue(nControlNode, VISIBILITY) != null) {
						final String val = XmlPaserUtil.getAttrValue(
								nControlNode, VISIBILITY);
						if (val.compareToIgnoreCase("N") == 0) {
							isVisibility = false;
						}
					}
					displayType = XmlPaserUtil.getAttrValue(nControlNode,
							DISPLAY_TYPE);
					// find minLength
					minLength = EditTextSpec.MIN_LENGTH;
					if (XmlPaserUtil.getAttrValue(nControlNode, MINLENGTH) != null) {
						final String val = XmlPaserUtil.getAttrValue(
								nControlNode, MINLENGTH);
						try {
							minLength = Integer.parseInt(val);
						} catch (final NumberFormatException e) {
							minLength = EditTextSpec.MIN_LENGTH;
						}
					}
					// find maxLength
					maxLenght = EditTextSpec.MAX_LENGTH;
					if (XmlPaserUtil.getAttrValue(nControlNode, MAXLENGTH) != null) {
						final String val = XmlPaserUtil.getAttrValue(
								nControlNode, MAXLENGTH);
						try {
							maxLenght = Integer.parseInt(val);
						} catch (final NumberFormatException e) {
							maxLenght = EditTextSpec.MAX_LENGTH;
						}
					}
					// find dataobj
					dataObject = XmlPaserUtil.getAttrValue(nControlNode,
							DATAOBJ);

					// find IsChecked
					if (XmlPaserUtil.getAttrValue(nControlNode, ISCHECKED) != null) {
						final String val = XmlPaserUtil.getAttrValue(
								nControlNode, ISCHECKED);
						if (val.equalsIgnoreCase("Y")) {
							isChecked = true;
						}
					}
					// find IsChecked
					if (XmlPaserUtil.getAttrValue(nControlNode,
							OBJ_LOAD_BY_CONFIG) != null) {
						final String val = XmlPaserUtil.getAttrValue(
								nControlNode, OBJ_LOAD_BY_CONFIG);
						if (val.equalsIgnoreCase("N")) {
							isLoadDataByObj = false;
						}
					}
					// find ValueProp
					selectWhere = XmlPaserUtil.getAttrValue(nControlNode,
							FILTER_BY);
					// find ContentData
					contentData = XmlPaserUtil.getAttrValue(nControlNode,
							CONTENTDATA);
					// find rptrDesign
					rptrDesign = XmlPaserUtil.getAttrValue(nControlNode,
							RPTRDESIGN);
					// find rowItem
					rowItem = XmlPaserUtil.getAttrValue(nControlNode, ROWITEM);
					header = XmlPaserUtil.getAttrValue(nControlNode, HEADER);
					footer = XmlPaserUtil.getAttrValue(nControlNode, FOOTER);
					// find regular expression
					regex = XmlPaserUtil.getAttrValue(nControlNode,
							REGULAR_EXPRESSION);
					// refresh control;
					refreshControl = XmlPaserUtil.getAttrValue(nControlNode,
							REFRESH_CONTROL);
					refreshValue = XmlPaserUtil.getAttrValue(nControlNode,
							REFRESH_VALUE);
					animation = XmlPaserUtil.getAttrValue(nControlNode,
							ANIMATION);
					// Control action;
					final String action = XmlPaserUtil.getAttrValue(
							nControlNode, ACTION);
					if (!TextUtils.isEmpty(action)) {
						actionType = ActionType.get(action);
					}

					if (XmlPaserUtil.getAttrValue(nControlNode,
							CURRENT_PAGE_INDEX) != null) {
						tabhostStartPageIndex = Integer
								.parseInt(XmlPaserUtil.getAttrValue(
										nControlNode, CURRENT_PAGE_INDEX));
					}
					tabhostBackgroundColor = XmlPaserUtil.getAttrValue(
							nControlNode, BACKGROUND_COLOR);

					tabhostDisplayStyle = XmlPaserUtil.getAttrValue(
							nControlNode, DISPLAY);

					linkingValueProp = XmlPaserUtil.getAttrValue(nControlNode,
							LINKING_VALUE_PROP);

					final String loadAll = XmlPaserUtil.getAttrValue(
							nControlNode, LOAD_ALL);
					if (!TextUtils.isEmpty(loadAll)
							&& loadAll.equalsIgnoreCase("Y")) {
						isLoadAll = true;
					}
				}

				if (nControlNode.getNodeType() == Node.ELEMENT_NODE) {
					final Element elementControlNode = (Element) nControlNode;
					// set ReadOnly
					final NodeList nodeReadOnly = elementControlNode
							.getElementsByTagName(ISREADONLY);
					if (nodeReadOnly.getLength() != 0) {
						isReadOnly = true;
					}

					// set validationGroup
					final NodeList nodeValidationGroup = elementControlNode
							.getElementsByTagName(VALIDATIONGROUP);
					if (nodeValidationGroup.getLength() != 0) {
						validationGroup = XmlPaserUtil
								.getNodeValue(nodeValidationGroup.item(0));
					}

					// find targets tag for ListView
					targetCollection = TargetCollectionPaser
							.parse(elementControlNode);
					// Parse Validation
					validation = ValidationParser
							.validationParser(elementControlNode);

					enableCondition = EnableConditionParser
							.parse(elementControlNode);
					final NodeList nodeValueList = elementControlNode
							.getElementsByTagName(VALUE_LIST);
					if (nodeValueList.getLength() != 0) {
						valueList = parseValueList(nodeValueList.item(0));
					}
				}
				final ControlType type = ControlType.get(controlType);
				element = (Element) nControlNode;
				if (type.equals(ControlType.GALLERY)) {
					formDesign.setGalleryID(controlId);
					subNodeList = element
							.getElementsByTagName(FormDesignParser.GALLERY_FORMAT);
					if (subNodeList.getLength() == 1) {
						controlSpec = GalleryFormat.parse(subNodeList.item(0));
						((GallerySpec) controlSpec).setValidation(validation);
						if (((GallerySpec) controlSpec).isUpload()) {
							formDesign.setHasGallery(true);
						}
					} else {
						final GallerySpec gallerySpec = GalleryPaser
								.parse(nControlNode);
						gallerySpec.setContentData(contentData);
						gallerySpec.setValidation(validation);
						gallerySpec.setImagePath(imagePath);
						gallerySpec.setReadOnly(isReadOnly);
						controlSpec = gallerySpec;
					}

				} else if (type.equals(ControlType.GRIDVIEW)) {
					subNodeList = element
							.getElementsByTagName(FormDesignParser.GRIDVIEW_FORMAT);
					if (subNodeList.getLength() == 1) {
						controlSpec = GridViewFormat.parse(subNodeList.item(0));
						((GridViewSpec) controlSpec)
								.setTargetCollection(targetCollection);
					} else {
						final GridViewSpec galleryFormat = parseGalleryFormat(nControlNode);
						galleryFormat.setContentData(contentData);
						galleryFormat.setDataObject(dataObject);
						galleryFormat.setRowItem(rowItem);
						galleryFormat.setRptrdesign(rptrDesign);
						controlSpec = galleryFormat;
					}
				} else if (type.equals(ControlType.VIEWPAGER)) {
					subNodeList = element
							.getElementsByTagName(FormDesignParser.VIEWPAGER_FORMAT);
					if (subNodeList.getLength() == 1) {
						controlSpec = ViewPagerParser
								.parse(subNodeList.item(0));
					} else {
						controlSpec = ViewPagerParser.parse(nControlNode);
					}
				} else if (type.equals(ControlType.TABHOST)) {
					subNodeList = element
							.getElementsByTagName(FormDesignParser.TABHOST_FORMAT);
					if (subNodeList.getLength() == 1) {
						controlSpec = TabHostFormat.parse(subNodeList.item(0));
					} else {
						final TabHostSpec tabHostFormat = new TabHostSpec();
						tabHostFormat.setPageCollection(PageCollectionParser
								.parse(nControlNode));
						tabHostFormat
								.setBackgroundColor(tabhostBackgroundColor);
						tabHostFormat.setDisplay(tabhostDisplayStyle);
						tabHostFormat.setStartTab(tabhostStartPageIndex);
						controlSpec = tabHostFormat;
					}
				} else if (type.equals(ControlType.EDITTEXT)) {
					subNodeList = element
							.getElementsByTagName(FormDesignParser.EDITTEXT_FORMAT);
					if (subNodeList.getLength() == 1) {
						final EditTextSpec editTextSpec = EditTextFormat
								.parse(subNodeList.item(0));
						editTextSpec.setValidation(validation);
						controlSpec = editTextSpec;
					} else {
						controlSpec = new EditTextSpec(isReadOnly, isSearch,
								validation, EditTextType.get(dataInputType),
								searchControl, searchFields, minLength,
								maxLenght, regex);
						((EditTextSpec) controlSpec)
								.setDisplayType(displayType);
						if (!TextUtils.isEmpty(displayFormat)) {
							((EditTextSpec) controlSpec)
									.setDisplayFormat(displayFormat);
						}
					}
				} else if (type.equals(ControlType.FLOATTING_EDITTEXT)) {
					subNodeList = element
							.getElementsByTagName(FormDesignParser.EDITTEXT_FORMAT);
					if (subNodeList.getLength() == 1) {
						final EditTextSpec editTextSpec = EditTextFormat
								.parse(subNodeList.item(0));
						editTextSpec.setValidation(validation);
						controlSpec = editTextSpec;
					} else {
						controlSpec = new EditTextSpec(isReadOnly, isSearch,
								validation, EditTextType.get(dataInputType),
								searchControl, searchFields, minLength,
								maxLenght, regex);
						((EditTextSpec) controlSpec)
								.setDisplayType(displayType);
						if (!TextUtils.isEmpty(displayFormat)) {
							((EditTextSpec) controlSpec)
									.setDisplayFormat(displayFormat);
						}
					}
				} else if (type.equals(ControlType.LISTVIEW)) {
					formDesign.setHasListView(true);
					formDesign.setListViewID(controlId);
					subNodeList = element
							.getElementsByTagName(FormDesignParser.LISTVIEW_FORMAT);
					if (subNodeList.getLength() == 1) {
						controlSpec = ListViewFormat.parse(subNodeList.item(0));
						((ListviewSpec) controlSpec)
								.setTargetCollection(targetCollection);
					} else {
						controlSpec = new ListviewSpec(rowItem, rptrDesign,
								contentData, targetCollection, header, footer,
								listViewSelection);
						((ListviewSpec) controlSpec).setLoadAll(isLoadAll);
						((ListviewSpec) controlSpec)
								.setLinkingValueProp(linkingValueProp);
						((ListviewSpec) controlSpec)
								.setDisplayProperty(displayProperty);
					}
				} else if (type.equals(ControlType.SPINNER)) {
					subNodeList = element
							.getElementsByTagName(FormDesignParser.SPINNER_FORMAT);
					if (subNodeList.getLength() == 1) {
						controlSpec = SpinnerFormat.parse(subNodeList.item(0));
					} else {
						controlSpec = SpinnerParser.parse(nControlNode);
						controlSpec.setValueList(valueList);
					}
				} else if (type.equals(ControlType.BUTTON)) {
					final ButtonSpec buttonSpec = new ButtonSpec(
							targetCollection);
					buttonSpec.setValidation(validation);
					controlSpec = buttonSpec;
				} else if (type.equals(ControlType.CHECKBOX)) {
					subNodeList = element
							.getElementsByTagName(FormDesignParser.CHECKBOX_FORMAT);
					if (subNodeList.getLength() == 1) {
						controlSpec = CheckBoxFormat.parse(subNodeList.item(0));
					} else {
						controlSpec = CheckBoxParser.parse(nControlNode);
					}
				} else if (type.equals(ControlType.RADIO_BUTTON)) {
					controlSpec = new RadioButtonSpec(isChecked, value);
				} else if (type.equals(ControlType.TEXTVIEW)) {
					controlSpec = new TextViewSpec(contentData);
					((TextViewSpec) controlSpec).setDisplayType(displayType);
					if (!TextUtils.isEmpty(displayFormat)) {
						((TextViewSpec) controlSpec)
								.setDisplayFormat(displayFormat);
					}
				} else if (type.equals(ControlType.DATETIME_PICKER)) {
					subNodeList = element
							.getElementsByTagName(FormDesignParser.DATETIME_PICKER_FORMAT);
					if (subNodeList.getLength() == 1) {
						controlSpec = DateTimeFormat.parse(subNodeList.item(0));
					}
				} else if (type.equals(ControlType.IMAGEVIEW)) {
					controlSpec = new ImageViewSpec(isReadOnly);
				} else if (type.equals(ControlType.SWITCH)) {
					subNodeList = element
							.getElementsByTagName(FormDesignParser.SWITCH_FORMAT);
					if (subNodeList.getLength() == 1) {
						controlSpec = SwitchFormat.parse(subNodeList.item(0));
					} else {
						controlSpec = SwitchParser.parse(nControlNode);
					}
				} else if (type.equals(ControlType.TOGGLE_BUTTON)) {
					subNodeList = element
							.getElementsByTagName(FormDesignParser.TOGGLE_FORMAT);
					if (subNodeList.getLength() == 1) {
						controlSpec = ToggleFormat.parse(subNodeList.item(0));
					}
				} else if (type.equals(ControlType.GOOGLE_MAPS)) {
					subNodeList = element
							.getElementsByTagName(FormDesignParser.GOOGLE_MAPS_FORMAT);
					if (subNodeList.getLength() == 1) {
						controlSpec = GoogleMapsFormat.parse(subNodeList
								.item(0));
					} else {
						controlSpec = new GoogleMapsSpec();
					}
				} else if (type.equals(ControlType.AUTOCOMPLETE_TEXTVIEW)) {
					subNodeList = element
							.getElementsByTagName(AUTOCOMPLETE_TEXTVIEW_FORMAT);
					if (subNodeList.getLength() == 1) {
						controlSpec = AutoCompleteTextViewFormat
								.parse(subNodeList.item(0));
						((AutoCompleteTextViewSpec) controlSpec)
								.setValidation(validation);
					} else {
						controlSpec = new AutoCompleteTextViewSpec();
					}
				} else if (type.equals(ControlType.FLOATING_AUTOCOMPLETE)) {
					subNodeList = element
							.getElementsByTagName(AUTOCOMPLETE_TEXTVIEW_FORMAT);
					if (subNodeList.getLength() == 1) {
						controlSpec = AutoCompleteTextViewFormat
								.parse(subNodeList.item(0));
						((AutoCompleteTextViewSpec) controlSpec)
								.setValidation(validation);
					} else {
						controlSpec = new AutoCompleteTextViewSpec();
					}
				}

				if (!TextUtils.isEmpty(selectWhere)) {
					controlSpec.setFilterBy(selectWhere);
				}

				controlSpec.setLoadByConfig(isLoadDataByObj);
				controlSpec.setRefreshControl(refreshControl);
				controlSpec.setRefreshValue(refreshValue);
				controlSpec.setControlId(controlId);
				controlSpec.setControlType(controlType);
				controlSpec.setDataObject(dataObject);
				controlSpec.setMappingName(mappingName);
				controlSpec.setValidationGroup(validationGroup);
				controlSpec.setValue(value);
				controlSpec.setControlStatus(controlStatus);
				controlSpec.setVisibility(isVisibility);
				controlSpec.setObjectBaseRule(objectBaseRule);
				if (!controlSpec.isToggleOfflineMode()) {
					controlSpec.setToggleOfflineMode(isToggleOfflineMode);
				}
				if (!controlSpec.isToggleAutoSync()) {
					controlSpec.setToggleAutoSync(isToggleAutoSync);
				}
				controlSpec.setActionType(actionType);
				controlSpec.setEnableCondition(enableCondition);

				controlSpec.setAnimation(animation);
				if (controlSpec.getControlType().equals(ControlType.LISTVIEW)) {
					if (TextUtils.isEmpty(controlSpec.getIDName())) {
						controlSpec.setControlId("android.R.id.list");
					}
					formDesign.getControlSpecCollection().put(
							controlSpec.getIDName(), controlSpec);
				} else if (controlSpec.getControlType().equals(
						ControlType.DATETIME_PICKER)) {
					formDesign.getControlSpecCollection().put("DateTimePicker",
							controlSpec);
				} else if (!TextUtils.isEmpty(controlSpec.getIDName())) {
					formDesign.getControlSpecCollection().put(
							controlSpec.getIDName(), controlSpec);
				}
			}
		}
		return formDesign;
	}
}
